<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 资源中心->用户信息
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row :gutter="10">
                    <el-col :xs="24" :sm="12" :lg="6">
                        <el-input v-model="query.data.username" placeholder="用户名称" class="handle-input"></el-input>
                    </el-col>
                    <el-col :xs="24" :sm="12" :lg="6">
                        <el-select v-model="query.data.userType" placeholder="用户类型" class="handle-select" clearable>
                            <el-option v-for="item in userTypeList"
                                       :key="item.value"
                                       :label="item.label"
                                       :value="item.value">
                                <span style="float: left">{{ item.label }}</span>
                            </el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" style="text-align: left">
                        <el-button type="primary" icon="el-icon-search" @click="handleAdd">新增</el-button>
                    </el-col>
                    <el-col :xs="24" :sm="12" style="text-align: right">
                        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="clearQueryForm">重置</el-button>
                    </el-col>
                </el-row>
            </div>

            <el-table border class="table"
                      ref="multipleTable"
                      :data="tableData"
                      header-cell-class-name="table-header"
                      @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="租户ID" width="80"></el-table-column>
                <el-table-column prop="userName" label="用户名称" width="180" align="center"></el-table-column>
                <el-table-column prop="userNick" label="用户昵称"></el-table-column>
                <el-table-column prop="userType" label="用户类型" align="center"></el-table-column>
                <el-table-column prop="openTime" label="开通时间" :formatter="tableDateFormat" align="center"></el-table-column>
                <el-table-column prop="closeTime" label="关闭时间" :formatter="tableDateFormat" align="center"></el-table-column>
                <el-table-column prop="status" label="状态" align="center"></el-table-column>
                <el-table-column label="操作" width="130" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit"
                                   @click="handleEdit(scope.$index, scope.row)">
                            编辑
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                                   @click="handleDelete(scope.$index, scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination background
                               layout="total, prev, pager, next"
                               :current-page="query.pageVo.pageNo"
                               :page-size="query.pageVo.pageSize"
                               :total="pageTotal"
                               @current-change="handlePageChange">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="租户信息管理" :visible.sync="editVisible" width="60%" :lock-scroll="false">

            <el-form ref="form" :model="formData" label-width="70px">
                <el-form-item label="租户编码">
                    <el-input v-model="formData.tenantCode"></el-input>
                </el-form-item>
                <el-form-item label="租户名称">
                    <el-input v-model="formData.tenantName"></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-radio-group v-model="formData.tenantType">
                        <el-radio-button v-for="item in tenantTypeList" :label="item.value">
                            {{ item.label }}
                        </el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="开通时间">
                    <el-date-picker
                            v-model="formData.openTime"
                            type="date"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="关闭时间">
                    <el-date-picker
                            v-model="formData.closeTime"
                            type="date"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="formData.remark"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleSave">保 存</el-button>
                <el-button @click="editVisible = false">取 消</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import { fetchDictByIndex } from '../../api/resic/dictionary';
    import {doFindTenant, doGetTenant, doSaveTenant, doDeleteTenant} from '../../api/resic/tenant.js';

    export default {
        name: 'basetable',
        data() {
            return {
                query: {
                    data:{
                        tenantCode: '',
                        tenantName: '',
                        tenantType: ''
                    },
                    pageVo:{
                        pageNo: 1,
                        pageSize: 10
                    }
                },
                pageTotal: 0,
                editVisible: false,
                formData: {},
                tableData:[],
                tenantTypeList:[],
                id: -1,
                idx: -1
            };
        },
        //初始化页面数据
        created() {
            this.initData();
            this.findPage();
        },

        //方法区
        methods: {
            dateFormat(row,column){
                let data = row[column.property]
                if (data ===null) {
                    return ''
                }
                let dt = new Date(data)
                return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
            },
            //初始化页面数据
            initData(){
                fetchDictByIndex("onion.tenant.type").then(res => {
                    this.tenantTypeList = res.data["onion.tenant.type"];
                });
            },
            //分页列表查询
            findPage(){
                doFindTenant(this.query).then(result=>{
                    this.tableData = result.data.rows;
                    this.pageTotal = result.data.total || 50;
                })
            },
            //查询按钮事件
            handleQuery(){
                this.findPage();
            },
            //新增租户
            handleAdd(){
                this.idx = -1;
                this.formData ={"tenantType":"corporation"}
                this.editVisible = true;
            },
            //编辑租户
            handleEdit(index, row) {
                this.idx = index;
                this.formData = row;
                this.editVisible = true;
            },
            //删除租户
            handleDelete(index, row){
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    doDeleteTenant(this.tableData[index].id).then(result => {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                    });
                }).catch(() => {
                    this.$alert('异常！');
                });
            },
            //保存租户信息
            handleSave(){
                doSaveTenant(this.formData).then(result=>{
                    this.$message.success(`保存成功！`);
                    if(this.idx != -1){
                        this.$set(this.tableData, this.idx, result.data);
                    }else{
                        this.findPage();
                    }
                    this.editVisible = false;
                });
            },
            //清空QueryForm
            clearQueryForm(){
                this.query.data = {};
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.findPage();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 10px;
    }

    .handle-select {
        width: 100%;
    }

    .handle-input {
        width: 100%;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }

    .clearfix:before,
    .clearfix:after {
        display: table;
        content: "";
    }
    .clearfix:after {
        clear: both
    }

</style>